package com.pshcp.doctor.api;

import com.alibaba.fastjson.JSONObject;
import com.external.hospital.ap.Hospital2UserApService;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.common.config.CustomConf;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.framework.util.DateUtil;
import com.framework.util.SecurityUtil;
import com.google.gson.JsonObject;
import com.pshcp.doctor.ao.personalinformation.SaveDoctorGoodAtAO;
import com.pshcp.doctor.ao.personalinformation.SaveDoctorHeadPhotoAO;
import com.pshcp.doctor.ao.personalinformation.SaveDoctorReusmeAO;
import com.pshcp.doctor.tool.DoctorPersonalInformationTool;
import com.pshcp.doctor.vo.personalinformation.DoctorHeadPhotoVO;
import com.pshcp.doctor.vo.personalinformation.UserProfileSummaryDataVO;
import com.pshcp.doctor.vo.personalinformation.UserProfileVO;
import com.pshcp.domain.model.Dept;
import com.pshcp.domain.model.Doctor;
import com.pshcp.domain.model.Session;
import com.pshcp.domain.model.SmsCaptcha;
import com.pshcp.service.DeptService;
import com.pshcp.service.DoctorDiagnosisEvaluationService;
import com.pshcp.service.DoctorService;
import com.pshcp.service.PatientFavorService;
import com.pshcp.service.PatientOnlineOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/4 09:48
 */
@Api(tags = {"医生端-个人信息接口"})
@Slf4j
@RestController
@RequestMapping("/api/doctor/personalInformation")
public class DoctorPersonalInformationAPI {

    @Autowired
    private CustomConf customConf;

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private PatientFavorService patientFavorService;

    @Autowired
    private DoctorDiagnosisEvaluationService doctorDiagnosisEvaluationService;

    @Autowired
    private PatientOnlineOrderService onlineOrderService;

    @Autowired
    private Hospital2UserApService hospital2UserApService;

    @Autowired
    private DeptService deptService;

    @ApiOperation("获取我的个人信息")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取我的个人信息")
    @PostMapping("getUserProfile")
    public ResultDTO<UserProfileVO> getUserProfile() throws BizException {
        Doctor doctor = doctorService.findById(ReqComTool.getUserId());
        List<Dept> deptList = deptService.getDeptListByDoctorId(ReqComTool.getUserId());
        UserProfileVO userProfileVO = DoctorPersonalInformationTool.buildUserProfileVO(customConf.getFileUrl(), doctor, deptList);
        return ResultBuilder.success(userProfileVO);
    }

    @ApiOperation("获取我的头像")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取我的头像")
    @PostMapping("getDoctorHeadPhoto")
    public ResultDTO<DoctorHeadPhotoVO> getDoctorHeadPhoto() throws BizException {
        Doctor doctor = doctorService.findById(ReqComTool.getUserId());
        DoctorHeadPhotoVO doctorHeadPhotoVO = DoctorPersonalInformationTool.buildDoctorHeadPhoto(customConf.getFileUrl(), doctor);
        return ResultBuilder.success(doctorHeadPhotoVO);
    }

    @ApiOperation("更新我的头像")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "更新我的头像")
    @PostMapping("saveDoctorHeadPhoto")
    public ResultDTO<Long> saveDoctorHeadPhoto(@RequestBody SaveDoctorHeadPhotoAO saveDoctorHeadPhotoAO) throws BizException {
        CommonTool.checkString(saveDoctorHeadPhotoAO.getHeadPhotoPath(), "10100001");
        Doctor doctor = doctorService.findById(ReqComTool.getUserId());
        Doctor updateDoctor = DoctorPersonalInformationTool.buildSaveDoctorHeadPhoto(saveDoctorHeadPhotoAO.getHeadPhotoPath(), doctor);
        doctorService.update(updateDoctor);
        return ResultBuilder.success(updateDoctor.getId());
    }

    @ApiOperation("获取我的个人简介")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取我的个人简介")
    @PostMapping("getDoctorReusme")
    public ResultDTO<String> getDoctorReusme() throws BizException {
        Doctor doctor = doctorService.findById(ReqComTool.getUserId());
        return ResultBuilder.success(doctor.getReusme());
    }

    @ApiOperation("更新我的个人简介")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "更新我的个人简介")
    @PostMapping("saveDoctorReusme")
    public ResultDTO<Long> saveDoctorReusme(@RequestBody SaveDoctorReusmeAO saveDoctorReusmeAO) throws BizException {
        CommonTool.checkString(saveDoctorReusmeAO.getReusme(), "10100002");
        Doctor doctor = doctorService.findById(ReqComTool.getUserId());
        Doctor updateDoctor = DoctorPersonalInformationTool.buildSaveDoctorReusme(saveDoctorReusmeAO.getReusme(), doctor);
        doctorService.update(updateDoctor);
        return ResultBuilder.success(updateDoctor.getId());
    }

    @ApiOperation("获取我的擅长标签")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取我的擅长标签")
    @PostMapping("getDoctorGoodAt")
    public ResultDTO<String> getDoctorGoodAt() throws BizException {
        Doctor doctor = doctorService.findById(ReqComTool.getUserId());
        return ResultBuilder.success(doctor.getGoodAt());
    }

    @ApiOperation("更新我的擅长标签")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "更新我的擅长标签")
    @PostMapping("saveDoctorGoodAt")
    public ResultDTO<Long> saveDoctorGoodAt(@RequestBody SaveDoctorGoodAtAO saveDoctorGoodAtAO) throws BizException {
        String doctorGoodAt = DoctorPersonalInformationTool.buildDoctorGoodAt(saveDoctorGoodAtAO.getGoodAt());
        Doctor doctor = doctorService.findById(ReqComTool.getUserId());
        Doctor updateDoctor = DoctorPersonalInformationTool.buildSaveDoctorGoodAt(doctorGoodAt, doctor);
        doctorService.update(updateDoctor);
        return ResultBuilder.success(updateDoctor.getId());
    }

    @ApiOperation("获取我的概况数据")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取我的概况数据")
    @PostMapping("getUserProfileSummaryData")
    public ResultDTO<UserProfileSummaryDataVO> getUserProfileSummaryData() throws BizException {
        int favorCount = patientFavorService.doctorFavorNum(ReqComTool.getUserId());
        int diagnosisEvaluationCount = doctorDiagnosisEvaluationService.doctorDiagnosisEvaluationNum(ReqComTool.getUserId());
        //咨询量
        Doctor doctor = doctorService.findById(ReqComTool.getUserId());
        // 患者数量
        Map<String, String> map = new HashMap<>();
        map.put("doctorId", ReqComTool.getUserId().toString());
        JSONObject jsonObject = hospital2UserApService.user2ap2patientSync2queryDoctorPatientToday(map);
        int patientNum = jsonObject.get("data") == null ? 0 : Integer.parseInt(jsonObject.get("data").toString());
        UserProfileSummaryDataVO userProfileSummaryDataVO = DoctorPersonalInformationTool.buildUserProfileSummaryData(favorCount, diagnosisEvaluationCount, patientNum);
        userProfileSummaryDataVO.setOnlineNum(doctor.getOnlineNum() == null ? 0 : doctor.getOnlineNum());
        return ResultBuilder.success(userProfileSummaryDataVO);
    }
}
